/**
 * @description 系统配置js-systemSetup
 * @author guoyanfei
 * @date 2018/04/09
 */
//项目路径
var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = "/gdp";
var myTextArea = document.getElementById('code_editor');
var myCodeMirror = CodeMirror.fromTextArea(myTextArea, {
    lineNumbers: true,
    matchBrackets: true,
    styleActiveLine: true,
    lineWrapping:true,
    theme:"neat",
    mode: 'text/apl',
    readOnly: false
});

$(function () {
    myCodeMirror.setValue("");
    getFiledList();
    $("#tree_refresh").click(function () {
        getFiledList();
    });

    $("#fileul").each(function () {
        $(this).on("click","li",function () {
            $(this).parent().find("li").each(function () {
                $(this).removeClass("liactive");
            });
            $(this).addClass("liactive");
            getFileContext($(this).html());
        });
    });

    $("#btn_save").click(function () {
       $.ajax({
           type:'post',
           url:postPath+'/system/systemSetup/saveFileContext',
           data:{filename:$("#hid_filename").val(),filecontext:myCodeMirror.getValue()},
           success:function (data) {
               bootbox.alert("保存成功后，会重新启动程序，请等待几秒！");
           }
       });
    });

});

//加载系统属性文件列表
function getFiledList(){
    $.ajax({
        type:'POST',
        url:postPath+'/system/systemSetup/getFileList',
        async:false,
        success:function (data) {
            $(".feeds").html("");
            $.each(data,function (i,e) {
                $(".feeds").append("<li>"+e+"</li>");
            });
        }
    });
}

//通过文件名称获取文件内容
function getFileContext(filename) {
    $("#hid_filename").val(filename);
    $.ajax({
        type:'POST',
        url:postPath+'/system/systemSetup/getFileContext',
        data:{filename:filename},
        success:function (data) {
            myCodeMirror.setValue(data);
        }
    });
}
